import { Bus, DAY_STATE, KLINE_RANK_FLUCTUATION, KLINE_RANK_FLUCTUATION_MN5, KLINE_RANK_FLUCTUATION_ALL } from "./Bus";
import { IBroadcast } from "./IBroadcast";

/**
 * 右侧币种的大盘数据
 */
export class WaveSideBroadcast extends IBroadcast {
    channelStr = "";
    channelParam = {
        interval: "D1",
        group: "TOP20",
        event: "subscribe",
        topic: "FLUCTUATION"
    };
    constructor() {
        super();
    }

    changeParam(timer: string) {
        console.warn("波动率---------");
        this.channelParam.interval = timer == "S1" ? "D1" : timer;
        this.subscribe();
    }
    
    subscribe(): void {
        console.warn('初始化订阅======================#################')
        this.unSubscribe();
        if (!this.channelParam) {
            console.log("订阅参数未初始化");
            return;
        }
        this.channelStr = JSON.stringify(this.channelParam);
        this.send(this.channelStr);
    }

    unSubscribe(): void {
        if (!this.channelStr) return;
        const obj = JSON.parse(this.channelStr);
        obj.event = "unsubscribe";
        this.send(JSON.stringify(obj));
    }

    handle(res: any): void {
        if(res.group == "TOP20"){
            if (res.data) {
                Bus.emit(KLINE_RANK_FLUCTUATION_MN5, res.data);
            }
        }
    }
}
